<script setup>
import { ref } from 'vue'
import { ElMessage } from 'element-plus'

// 导入路由对象
import { useRouter } from 'vue-router'
const router = useRouter()

// 导入修改密码的api
import { userUpdatePassService } from '@/api/user.js'
const formModel = ref({
  old_pwd: '',
  new_pwd: '',
  re_pwd: ''
})

// 组件的验证规则
const rules = {
  old_pwd: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { pattern: /^\S{6,15}$/, message: '密码必须是6-15位的非空字符' }
  ],
  new_pwd: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { pattern: /^\S{6,15}$/, message: '密码必须是6-15位的非空字符' }
  ],
  re_pwd: [
    { required: true, message: '请再次输入密码', trigger: 'blur' },
    {
      pattern: /^\S{6,15}$/,
      message: '密码必须是6-15的非空字符',
      trigger: 'blur'
    },
    {
      validator: (rule, value, callback) => {
        if (value !== formModel.value.new_pwd) {
          callback(new Error('两次输入密码不一致!'))
        } else {
          callback()
        }
      },
      trigger: 'blur'
    }
  ]
}

// 提交密码修改
const sub_p_word = async () => {
  const res =  await userUpdatePassService(formModel.value)
  ElMessage.success('修改密码成功请重新登录')
  router.push('/login')
  console.log(res);
}
</script>

<template>
  <PageContainer title="重置密码">
    <el-form :model="formModel" :rules="rules">
      <el-form-item label="原密码" prop="old_pwd">
        <el-input
          style="width: 719px; padding: 1px 15px"
          :prefix-icon="User"
          v-model="formModel.old_pwd"
        ></el-input>
      </el-form-item>
      <el-form-item label="新密码" prop="new_pwd">
        <el-input
          style="width: 719px; padding: 1px 15px"
          :prefix-icon="User"
          v-model="formModel.new_pwd"
        ></el-input>
      </el-form-item>
      <el-form-item label="确认新密码" prop="re_pwd">
        <el-input style="width: 719px" :prefix-icon="User" v-model="formModel.re_pwd"></el-input>
      </el-form-item>
      <el-form-item style="margin-left: 100px">
        <el-button class="submit" size="large" type="primary" @click="sub_p_word"
          >提交修改</el-button
        >
        <el-button size="large">重置</el-button>
      </el-form-item>
    </el-form>
  </PageContainer>
</template>
